﻿//==============================================================================
// File: Common.xaml.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WpfRcp.Resources
{
    public partial class Common : ResourceDictionary
    {
        public Common()
        {
            InitializeComponent();
        }
    }

    public class ColorBrushConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Color color = (Color)value;
            return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        #endregion
    }

    /// <summary>
    /// Reverse engineered converter
    /// </summary>
    public class ProgressBarHighlightConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Type type = typeof(double);
            if ((((values == null) || (values.Length != 3)) || ((values[0] == null) || (values[1] == null))) || (((values[2] == null) || !typeof(Brush).IsAssignableFrom(values[0].GetType())) || (!type.IsAssignableFrom(values[1].GetType()) || !type.IsAssignableFrom(values[2].GetType()))))
            {
                return null;
            }
            Brush brush = (Brush)values[0];
            double d = (double)values[1];
            double num2 = (double)values[2];
            if ((((d <= 0) || double.IsInfinity(d)) || (double.IsNaN(d) || (num2 <= 0))) || (double.IsInfinity(num2) || double.IsNaN(num2)))
            {
                return null;
            }
            DrawingBrush brush2 = new DrawingBrush();
            double width = d * 2;
            brush2.Viewport = brush2.Viewbox = new Rect(-d, 0, width, num2);
            brush2.ViewportUnits = brush2.ViewboxUnits = BrushMappingMode.Absolute;
            brush2.TileMode = TileMode.None;
            brush2.Stretch = Stretch.None;
            DrawingGroup group = new DrawingGroup();
            DrawingContext context = group.Open();
            context.DrawRectangle(brush, null, new Rect(-d, 0, d, num2));
            TimeSpan keyTime = TimeSpan.FromSeconds(width / 200);
            TimeSpan span2 = TimeSpan.FromSeconds(1);
            DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames
                                                          {
                                                              BeginTime = TimeSpan.Zero,
                                                              Duration = new Duration(keyTime + span2),
                                                              RepeatBehavior = RepeatBehavior.Forever
                                                          };
            animation.KeyFrames.Add(new LinearDoubleKeyFrame(width, keyTime));
            TranslateTransform transform = new TranslateTransform();
            transform.BeginAnimation(TranslateTransform.XProperty, animation);
            brush2.Transform = transform;
            context.Close();
            brush2.Drawing = group;
            return brush2;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }

    /// <summary>
    /// Converts value with exponential scale to percents
    /// </summary>
    public class Double2PercentConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double doubleValue = (double)value;
            return (Math.Pow(2.0, doubleValue) * 100.0).ToString("F0") + "%";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class DoubleToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToInt32(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToDouble(value);
        }
    }

    public class AboutBoxBorderConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int index = (int) value;
            if (index == 0)
                return new CornerRadius(0, 5, 5, 5);
            return new CornerRadius(5, 5, 5, 5);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}